/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.example.netcracker;

import java.util.Locale;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import ru.example.netcracker.controller.ControllerFacade;
import ru.example.netcracker.controller.TaskManagerController;
import ru.example.netcracker.model.ModelFacade;
import ru.example.netcracker.model.observer.TaskManagerModel;
import ru.example.netcracker.ui.MainFrame;
//import sun.net.www.protocol.http.logging.HttpLogFormatter;

/**
 *
 * @author Nikolay
 */
public class Startup {

    private static final Logger LOG = Logger.getLogger(Startup.class.getName());

    public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH);
        FileSettings settings = FileSettings.getInstance();
        if(args.length > 0){            
            settings.loadSettings(args[1]);
            if(args.length > 1) {                
                PropertyConfigurator.configure(args[1]);
            }            
        } else {
            BasicConfigurator.configure();
            settings.loadSettings("resources/config.properties");
        }
        
        try {
            javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            LOG.log(Level.ERROR, "Exception: ", ex);
        }
        
        PropertyConfigurator.configure("logger.configure");
        settings.loadSettings("resources/config.properties");
        //System.out.println(settings.toString());

        final ModelFacade model = new TaskManagerModel();
        final ControllerFacade ctrl = new TaskManagerController(model);
        final MainFrame view = MainFrame.newInstance(ctrl);        
        ctrl.addOnReceiveTaskListener(view);
        ctrl.addObserver(view);
        ctrl.init();
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                view.setVisible(true);
            }
        });
    }
}
